{ "cells": [ { "cell_type": "markdown", "id": "8407f552-789a-4efd-9f60-8ed0892adc83", "metadata": {}, "source": [ "(basic_output_ex)=\n", "# Basic Output Example\n", "\n", "This example show the simplest way to go from a function and set of parameter\n", "combinations to a labelled {class}`xarray.Dataset`.\n", "\n", "Imagine we want to explore the function {func}`scipy.special.eval_jacobi`. It takes four different arguments and we want to get a feel for what each does. First we wrap it in a {class}`~xyzpy.Runner` object, that encapsulates how to run all the different combinations of its arguments and automatically labels the output." ] }, { "cell_type": "code", "execution_count": 1, "id": "75dd6cf1-9ba4-4d55-a256-5c1fe31f8610", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", " fn: \n", " fn_args: ('x', 'n', 'alpha', 'beta')\n", " var_names: ('Pn(x)',)\n", " var_dims: {'Pn(x)': ()}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%config InlineBackend.figure_formats = ['svg']\n", "from scipy.special import eval_jacobi\n", "\n", "import xyzpy as xyz\n", "\n", "\n", "def jacobi(x, n, alpha, beta):\n", " return eval_jacobi(n, alpha, beta, x)\n", "\n", "\n", "r = xyz.Runner(jacobi, var_names=\"Pn(x)\")\n", "r" ] }, { "cell_type": "markdown", "id": "09f2512a-cc95-46a0-ad8c-65e05b7bf878", "metadata": {}, "source": [ "This is as simple as it gets, the function ``jacobi`` has one output variable, which we are calling ``'Pn(x)'``.\n", "\n", "Now let's define all the different values we want to try for each argument (the function actually vectorizes over ``x`` so this is overkill, but serves as a good demonstration):" ] }, { "cell_type": "code", "execution_count": 2, "id": "ce117408-ca30-4f8e-9be1-08ec6773e160", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "combos = {\n", " \"x\": np.linspace(0, 1, 101),\n", " \"n\": [1, 2, 4, 8, 16],\n", " \"alpha\": np.linspace(0, 2, 3),\n", " \"beta\": np.linspace(0, 1, 5),\n", "}" ] }, { "cell_type": "markdown", "id": "0d254a72-1e88-4f2f-863a-cbdb5c5eb905", "metadata": {}, "source": [ "Now, letโ€™s run the function for every combination of the above parameters:" ] }, { "cell_type": "code", "execution_count": 3, "id": "0c0f3b05-52b1-4fef-8e8e-d46f3c57f947", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|##########| 7575/7575 [00:00<00:00, 594077.39it/s]\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset> Size: 62kB\n",
       "Dimensions:  (x: 101, n: 5, alpha: 3, beta: 5)\n",
       "Coordinates:\n",
       "  * x        (x) float64 808B 0.0 0.01 0.02 0.03 0.04 ... 0.97 0.98 0.99 1.0\n",
       "  * n        (n) int64 40B 1 2 4 8 16\n",
       "  * alpha    (alpha) float64 24B 0.0 1.0 2.0\n",
       "  * beta     (beta) float64 40B 0.0 0.25 0.5 0.75 1.0\n",
       "Data variables:\n",
       "    Pn(x)    (x, n, alpha, beta) float64 61kB 0.0 -0.125 -0.25 ... 153.0 153.0
" ], "text/plain": [ " Size: 62kB\n", "Dimensions: (x: 101, n: 5, alpha: 3, beta: 5)\n", "Coordinates:\n", " * x (x) float64 808B 0.0 0.01 0.02 0.03 0.04 ... 0.97 0.98 0.99 1.0\n", " * n (n) int64 40B 1 2 4 8 16\n", " * alpha (alpha) float64 24B 0.0 1.0 2.0\n", " * beta (beta) float64 40B 0.0 0.25 0.5 0.75 1.0\n", "Data variables:\n", " Pn(x) (x, n, alpha, beta) float64 61kB 0.0 -0.125 -0.25 ... 153.0 153.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.run_combos(combos)" ] }, { "cell_type": "markdown", "id": "07e0f71e-df88-4dfa-a463-e12682eaa1f6", "metadata": {}, "source": [ "The resulting dataset is stored in ``r.last_ds`` and is an automatically labelled n-dimensional `xarray.Dataset`. Let's interactively plot what we have, showing the effect of all four dimensions:" ] }, { "cell_type": "code", "execution_count": 4, "id": "9786a033-26e6-49ed-b3db-0e3e1439b916", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "fig, axs = r.last_ds.xyz.plot(\n", " x=\"x\",\n", " y=\"Pn(x)\",\n", " color=\"beta\",\n", " linestyle=\"beta\",\n", " col=\"n\",\n", " row=\"alpha\",\n", " marker=\"\",\n", " ylim=(-2, 2),\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3" } }, "nbformat": 4, "nbformat_minor": 4 }